package cc.openkit.admin.controller.common;

import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 这是上传的公共接口
 *
 * 1. 图片上传接口
 *
 *
 */

@Controller
@Scope("prototype")
@RequestMapping("/apiCommon")
public class ApiCommon {

    private Logger log = Logger.getLogger(ApiCommon.class);

    @Autowired
    private ServletContext servletContext;

    /**
     * 图片上传接口 -- 用于layui
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/setImage", method = RequestMethod.POST)
    @ResponseBody
    public Object getAbouts(HttpServletRequest request, @RequestParam("layuiFile") MultipartFile layuiFile) throws Exception {
        log.info("这里是保存图片的公共接口，我准备保存图片啦");
        // 保存前台传来的缩略图
        String img = saveFile(layuiFile); // 获取图片地址
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();

        map1.put("src",img);
        map.put("code",0);
        map.put("msg","");
        map.put("data", map1);
        return map;
    }


    /**
     * 把 HTTP 请求中的文件流保存到本地
     *
     * @param file MultipartFile 的对象
     */
    private String saveFile(MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                // getRealPath() 取得 WEB-INF 所在文件夹路径
                // 如果参数是 "/temp", 当 temp 存在时返回 temp 的本地路径, 不存在时返回 null/temp (无效路径)
                String name= KitUtil.uuid();
                // 截取文件格式
                String type1 = file.getContentType();
                String type = type1.substring(6);
                String path = servletContext.getRealPath("") + "/"+StaticFinalVar.IMG_FILES+  name+"."+type;
                FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(path));

                String imgurl = StaticFinalVar.IMG_FILES +  name+"."+type;

                System.out.println("file>>>>>>>>>>>>>"+name+"."+type);
                System.out.println(imgurl);
                return imgurl;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 图片上传 -- 用于我们的wangEditor
     * 多张图片上传
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/wUeSetImg")
    @ResponseBody
    public Object uploadFile(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) {

        Map<String, Object> map = new HashMap<String, Object>();
        String[] strings = new String[file.length];

        for(int i=0; i<file.length; i++){
            // 获取参数

            String url = saveFile(file[i]);
            if(url == null){
                map.put("errno",0);
                return JSONObject.toJSON(map);
            }
            strings[i]=url;
        }

        map.put("errno",0);
        map.put("data",strings);

        System.out.println(JSONObject.toJSON(map));

        return JSONObject.toJSON(map);
    }

    /**
     * 保存文件到七牛
     */




}
